package cn.itcast.search.mq;

import cn.itcast.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemListener {
    @Autowired
    private SearchService searchService;

    /**
     * 监听上架商品
     * @param id 商品id
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),
            value = @Queue(name = "item_up_queue"),
            key = "item.up"
    ))
    public void listenHotelInsertOrUpdate(Long id){
        searchService.insertById(id);
    }

    /**
     * 监听下架商品
     * @param id 商品id
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "item.topic",type = ExchangeTypes.TOPIC),
            value = @Queue(name = "item_down_queue"),
            key = "item.down"
    ))
    public void listenHotelDelete(Long id){
        searchService.deleteById(id);
    }
}
